package org.testfire.translator.watij;

import org.testfire.model.TasksType;
import org.testfire.model.TestFireObject;
import org.testfire.model.TestsetType;
import org.testfire.translator.TestFireTranslator;
import org.testfire.translator.TranslatorFactory;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 2, 2007
 * Time: 3:52:34 PM
 *
 */
public class TasksToWatijTranslator<T> implements TestFireTranslator<TasksType>
{

    /**
     * Translate a list of Tasks ( ElementActions, BrowserCommands, Assertions).
     * @see schema TasksType
     * @return The source code for a valid java class, containing all test cases.
     */
    public Object translate( TestsetType testset, TasksType tasks )
    {
        StringBuilder buf = new StringBuilder();
        for ( TestFireObject o : tasks.getChildren() )
        {
            String s = TranslatorFactory.get( o.getClass() ).translate( testset, o ).toString();
            buf.append( s ).append( ";\n" );
            if (s.indexOf( ".start(\"" ) > 0 )
            {
                 buf.append( "Link ieHatred = engine_0.link( id(\"overridelink\") );\n"
                + "        if(ieHatred.exists())\n"
                + "        {\n"
                + "            ieHatred.click();\n"
                + "        }\n" );
            }

        }
        return buf.toString();
    }
}